﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace eKanbanWPF.View
{
    /// <summary>
    /// Interaction logic for FrmTask.xaml
    /// </summary>
    public partial class FrmTask : Window
    {
        internal readonly static FrmTask frmTask = new FrmTask();
        static bool result = false;
        #region Properties
        public static string TaskName { get { return frmTask.txtTaskName.Text; } set { frmTask.txtTaskName.Text = value; } }
        public static int Priority { get { return (int)frmTask.spPriority.Value; } set { frmTask.spPriority.Value = value; } }
        public static DateTime Deadline { get { return frmTask.dateDeadline.DateTime; } set { frmTask.dateDeadline.DateTime = value; } }
        public static DateTime StartTime
        {
            get { return frmTask.dateStart.DateTime; }
            set
            {
                if (value > frmTask.dateFinish.DateTime)
                {
                    throw new ArgumentException("Start Time cannot be larger than Finish Time");
                }
                frmTask.dateStart.DateTime = value;
            }
        }
        public static DateTime FinishTime
        {
            get { return frmTask.dateFinish.DateTime; }
            set
            {
                if (value < frmTask.dateStart.DateTime)
                {
                    throw new ArgumentException("Finish Time cannot be smaller than Start Time");
                }
                frmTask.dateFinish.DateTime = value;
                if (value == DateTime.MaxValue)
                {
                    frmTask.dateFinish.Text = "Not finish yet";
                }
                else
                {
                    frmTask.dateFinish.IsEnabled = true;
                    frmTask.dateFinish.Text = "";
                }
            }
        }

        public static Model.Task.StatusType Status
        {
            get
            {
                switch (frmTask.cbStatus.SelectedIndex)
                {
                    case 1:
                        return Model.Task.StatusType.Completed;
                    case 2:
                        return Model.Task.StatusType.Defect;
                    case 3:
                        return Model.Task.StatusType.Blocked;
                        
                    default:
                        return Model.Task.StatusType.Ongoing;
                }
            }
            set
            {
                switch (value)
                {
                    case eKanbanWPF.Model.Task.StatusType.Ongoing:
                        frmTask.cbStatus.SelectedIndex = 0;
                        break;
                    case eKanbanWPF.Model.Task.StatusType.Completed:
                        frmTask.cbStatus.SelectedIndex = 1;
                        break;
                    case eKanbanWPF.Model.Task.StatusType.Blocked:
                        frmTask.cbStatus.SelectedIndex = 3;
                        break;
                    case eKanbanWPF.Model.Task.StatusType.Defect:
                        frmTask.cbStatus.SelectedIndex = 2;
                        break;
                    default:
                        break;
                }
            }
        }

        public static string Description { get { return frmTask.txtDescription.Text; } set { frmTask.txtDescription.Text = value; } }
        //public string[] SourceFeatures
        //{
        //    set
        //    {
        //        cboFeature.Items.Clear();
        //        //foreach (var item in value)
        //        //{
        //        //    cboFeature.Items.Add(item);
        //        //}
        //        cboFeature.ItemsSource = value;
        //        if (value != null)
        //        {
        //            cboFeature.SelectedItem = value[0];
        //        }
        //    }
        //}

        //public string Feature
        //{
        //    get { return cboFeature.SelectedItem as string; }
        //    set
        //    {
        //        cboFeature.SelectedItem = value;
        //    }
        //}

        public eKanbanWPF.Model.Feature[] SourceFeatures
        {
            set
            {
                cboFeature.Items.Clear();
                cboFeature.ItemsSource = value;
                if (value != null)
                {
                    cboFeature.SelectedItem = value[0];
                }
            }
        }

        public static eKanbanWPF.Model.Feature Feature
        {
            get { return frmTask.cboFeature.SelectedItem as eKanbanWPF.Model.Feature; }
            set
            {
                frmTask.cboFeature.SelectedItem = value;
            }
        }
        #endregion
        private FrmTask()
        {
            InitializeComponent();

            SetEvent();
            
            //cbStatus.Items.Add(Model.Task.StatusType.Ongoing);
            //cbStatus.Items.Add(Model.Task.StatusType.Completed);
            //cbStatus.Items.Add(Model.Task.StatusType.Defect);
            //cbStatus.Items.Add(Model.Task.StatusType.Blocked);
            statusBlocked.Tag = Model.Task.StatusType.Blocked;
            statusCompleted.Tag = Model.Task.StatusType.Completed;
            statusDefect.Tag = Model.Task.StatusType.Defect;
            statusOngoing.Tag = Model.Task.StatusType.Ongoing;
        }

        private void ResetControl()
        {
            txtTaskName.Text = "";
            txtDescription.Text = "";
            spPriority.Value = 1;
            dateFinish.Text = "Not finish yet";
            dateStart.DateTime = DateTime.Now.Date;
            dateDeadline.DateTime = DateTime.Now.Date;
            cboFeature.ItemsSource = null;
        }
        private void AssignData(eKanbanWPF.Model.Task task)
        {
            if (task != null)
            {
                TaskName = task.Name;
                Priority = task.Priority;
                Deadline = task.DeadLine;
                StartTime = task.StartTime;
                FinishTime = task.FinishTime;
                //Feature = task.Feature.Name;
                Feature = task.Feature;
                Description = task.Description;
                Status = task.Status;
            }
            else
            {
                ResetControl();
            }
        }

        public static bool Show(eKanbanWPF.Model.Task task)
        {
            result = false;
            frmTask.AssignData(task);
            var r = frmTask.ShowDialog();
            return result;
        }

        private void SetEvent()
        {
            App.Current.MainWindow.Closed += (sender, e) => Close();

            Closing += new System.ComponentModel.CancelEventHandler(FrmTask_Closing);

            btnOK.Click += new RoutedEventHandler(btnOK_Click);
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);

            txtTaskName.Validate += new DevExpress.Xpf.Editors.Validation.ValidateEventHandler(txtTaskName_Validate);
            txtDescription.Validate += new DevExpress.Xpf.Editors.Validation.ValidateEventHandler(txtDescription_Validate);

            dateStart.Validate += new DevExpress.Xpf.Editors.Validation.ValidateEventHandler(dateStart_Validate);
            dateFinish.Validate += new DevExpress.Xpf.Editors.Validation.ValidateEventHandler(dateFinish_Validate);
        }

        #region Events
        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Hide();
        }

        void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                DialogResult = true;
                result = true;
                e.Handled = true;
            }
        }

        void FrmTask_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (Visibility == System.Windows.Visibility.Visible)
            {
                Hide();
                e.Cancel = true;
            }

        }

        void txtDescription_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
        {
            var value = e.Value;

            e.IsValid = true;
            if (value == null || (value is string && ((string)value).Trim().Length == 0))
            {
                e.IsValid = false;
                e.ErrorContent = "Description cannot be empty.";
                e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical;
            }
        }

        void txtTaskName_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
        {
            var value = e.Value;

            e.IsValid = true;
            if (value == null || (value is string && ((string)value).Trim().Length == 0))
            {
                e.IsValid = false;
                e.ErrorContent = "Task's Name cannot be empty.";
                e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical;
            }
        }

        void dateFinish_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
        {
            e.IsValid = true;
            if (e.Value == null || (e.Value is DateTime && (DateTime)e.Value < dateStart.DateTime))
            {
                e.IsValid = false;
                e.ErrorContent = "Start Time cannot be larger than Finish Time";
                e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical;
            }
        }

        void dateStart_Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e)
        {
            e.IsValid = true;
            if (e.Value == null || (e.Value is DateTime && (DateTime)e.Value > dateFinish.DateTime))
            {
                e.IsValid = false;
                e.ErrorContent = "Start Time cannot be larger than Finish Time";
                e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical;
            }
        }
        #endregion

        private bool Validate()
        {
            return true;
        }
    }
}
